package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.service.AdminPiccNurseService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallRole;
import org.linlinjava.litemall.db.domain.PiccNurse;
import org.linlinjava.litemall.db.service.LitemallAdminService;
import org.linlinjava.litemall.db.service.LitemallRoleService;
import org.linlinjava.litemall.db.service.PiccNurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/nurse")
@Validated
public class PiccNurseController {
    private final Log logger = LogFactory.getLog(PiccNurseController.class);
    @Autowired
    PiccNurseService piccNurseService;
    @Autowired
    LitemallAdminService litemallAdminService;
    @Autowired
    AdminPiccNurseService adminPiccNurseService;
    @Autowired
    LitemallRoleService roleService;

    /**
     * 查询所有的护士信息list
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:nurse:list")
    @RequiresPermissionsDesc(menu = {"护士管理", "所有护士"}, button = "查询")
    @GetMapping("/list")
    public Object getPatients(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer limit,
                              @Sort @RequestParam(defaultValue = "id") String sort,
                              @RequestParam(defaultValue = "desc") String order){
        logger.debug("enter list");
        List<PiccNurse> mList = piccNurseService.getNurseList(page, limit, sort, order);
        logger.debug(mList);
        return ResponseUtil.ok(mList);
    }

    /**
     * 返回一个护士的所有字段，根据id查询
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object getDetail(@RequestParam Integer id){
        PiccNurse p = piccNurseService.getDetailById(id);
        return ResponseUtil.ok(p);
    }

    /**
     * 返回一个护士的所有字段，根据id查询
     * @param jobNum
     * @return
     */
    @GetMapping("/detailbynum")
    public Object getDetailByNum(@RequestParam String jobNum){
        PiccNurse p = piccNurseService.getDetailByJobNum(jobNum);
        return ResponseUtil.ok(p);
    }

    /**
     * 添加或更新护士
     * @param record
     * @return
     */
    @PostMapping("/create")
    public Object add(@RequestBody PiccNurse record){
        logger.debug("enter nurse create");
        PiccNurse p = adminPiccNurseService.add(record);
        return ResponseUtil.ok(p);
    }

    /**
     * 添加或更新护士
     * @param record
     * @return
     */
    @PostMapping("/verify")
    public Object verify(@RequestBody PiccNurse record){
        logger.debug("enter nurse verify");
        record.setStatus(true);
        PiccNurse nurse = piccNurseService.update(record);

        //每次验证护士成功之后，就会自动为这个护士创建一个用户名为工号，密码为123456的管理员
        LitemallAdmin admin = new LitemallAdmin();
        String rawPassword = "123456";
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(rawPassword);
        admin.setPassword(encodedPassword);
        admin.setUsername(nurse.getJobNumber());

        LitemallRole role = roleService.findByName("nurse");
        if (role!=null) {
            Integer[] roleIds = new Integer[1];
            roleIds[0] = role.getId();
            admin.setRoleIds(roleIds);
            litemallAdminService.add(admin);
        }

        return ResponseUtil.ok(nurse);
    }
}
